/**
 * Copyright 2013 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2013-8-27 上午9:49:42
 */
package com.absir.android.view;

import android.content.ContextWrapper;
import android.view.LayoutInflater;
import android.view.View;

/**
 * @author absir
 * 
 */
public class ViewHolder extends ContextWrapper {

	/** tag */
	private Object tag;

	/** init */
	private boolean init;

	/** contentView */
	protected View contentView;

	/**
	 * 
	 */
	public ViewHolder() {
		super(InjectViewUtils.getApplication());
	}

	/**
	 * @return the tag
	 */
	public Object getTag() {
		return tag;
	}

	/**
	 * @param tag
	 *            the tag to set
	 */
	public void setTag(Object tag) {
		this.tag = tag;
	}

	/**
	 * 
	 */
	public synchronized final void initialize() {
		if (init == false) {
			init = true;
			onInitialize();
		}
	}

	/**
	 * 
	 */
	protected void onInitialize() {
		InjectViewUtils.inject(this);
	}

	/**
	 * @return the contentView
	 */
	public View getContentView() {
		if (init == false) {
			initialize();
		}

		return contentView;
	}

	/**
	 * @param layoutId
	 */
	public void setContentView(int layoutId) {
		setContentView(LayoutInflater.from(this).inflate(layoutId, null));
	}

	/**
	 * @param view
	 */
	public void setContentView(View view) {
		contentView = view;
		contentView.setTag(this);
		initialize();
	}
}
